package com.st.mall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mysql.cj.util.StringUtils;
import com.st.common.dubboProvider.product.GetSkuNamesService;
import com.st.common.dubboProvider.ware.JudgeWareSkuService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.st.common.utils.PageUtils;
import com.st.common.utils.Query;

import com.st.mall.ware.dao.WareSkuDao;
import com.st.mall.ware.entity.WareSkuEntity;
import com.st.mall.ware.service.WareSkuService;


@Service("wareSkuService")
@DubboService(interfaceClass = JudgeWareSkuService.class)
public class WareSkuServiceImplService extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService, JudgeWareSkuService {
    @Autowired
    WareSkuDao wareSkuDao;


    @DubboReference(check = false)
    private GetSkuNamesService getSkuNamesService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<WareSkuEntity> wrapper = new LambdaQueryWrapper<>();
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isNullOrEmpty(wareId)) {
            wrapper.eq(WareSkuEntity::getWareId, wareId);
        }
        String skuId = (String) params.get("skuId");
        if (!StringUtils.isNullOrEmpty(skuId)) {
            wrapper.eq(WareSkuEntity::getSkuId, skuId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params)
                , wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //若仓库以前没有这种商品，则新增
        List<WareSkuEntity> entities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (entities == null || entities.size() == 0) {
            WareSkuEntity skuEntity = new WareSkuEntity();
            skuEntity.setSkuId(skuId);
            skuEntity.setStock(skuNum);
            skuEntity.setWareId(wareId);
            skuEntity.setStockLocked(0);
            // 远程查询sku的名字

            //TODO 还可以用什么办法让异常出现以后不回滚？除了try，cathc
            // 如果远程调用失败，为防止整个事务回滚，加try，catch
            try {
//                R info = productFeignService.info(skuId);
//                Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");
//
//                if(info.getCode() == 0){
//                    skuEntity.setSkuName((String) data.get("skuName"));
//                }
                String name = getSkuNamesService.getSkuInfo(skuId);
                if (!StringUtils.isNullOrEmpty(name)) {
                    skuEntity.setSkuName(name);
                }
            } catch (Exception e) {

            }

            wareSkuDao.insert(skuEntity);
        } else {
            wareSkuDao.addStock(skuId, wareId, skuNum);
        }
    }

    @Override
    public Map<String, Boolean> isHaveWare(Long skuId) {
        boolean flag = false;
        Map<String, Boolean> map = new HashMap<>();
        WareSkuEntity byId = this.getById(skuId);
        //有库存
        if (byId != null && byId.getStock() > 0) {
            flag = true;
        }
        map.put(String.valueOf(skuId), flag);
        return map;
    }
}